//
//  MyBaseInfoVC.m
//  JinFuThree
//
//  Created by 毛韶谦 on 2018/9/3.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "MyBaseInfoVC.h"
#import "BaseInfoOneCell.h"
#import "BaseInfoTwoCell.h"
#import "BaseInfoThreeCell.h"
#import "MyInfoModel.h"
#import "MyRequestTools.h"

@interface MyBaseInfoVC ()<UITableViewDataSource, UITableViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) MyInfoBasicsModel *basicsModel;
@property(nonatomic,strong)NSArray *marriedDataArr;

@end

@implementation MyBaseInfoVC

{
    NSInteger selectRow;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavigationBar];
    [self initUI];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self updateData];
}

- (void)initNavigationBar {
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setImage:[[UIImage imageNamed:@"BackArrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    button1.frame = CGRectMake(0, 2.5, 30, 41.5f);
    [button1 addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    [backgroundView addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button2 setImage:[[UIImage imageNamed:@"nav_close"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    button2.frame = CGRectMake(30, 2.5, 30, 41.5f);
    [button2 addTarget:self action:@selector(backRootAction) forControlEvents:UIControlEventTouchUpInside];
    
    [backgroundView addSubview:button2];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backgroundView];
}
- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)backRootAction {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)initUI {
    
    self.title = @"基本信息";
    self.mainTableView.backgroundColor = ListMaoBackgroundColor;
}


- (void)updateData {
    
    [HFQIndicationView showInView:self.view];
    [MyRequestTools MyInfoGetBasicWithParameters:nil Success:^(id  _Nullable responseObject) {
        [HFQIndicationView dismiss];
        NSDictionary *dataDic = responseObject[@"data"];
        [self.basicsModel setValuesForKeysWithDictionary:dataDic];
        [self.mainTableView reloadData];
        
    } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        [HFQIndicationView dismiss];
        
    }];
}


#pragma mark tableView代理方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 5;
    }else if (section == 1) {
        return 1;
    }else {
        return 1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 175;
        }else {
            return 50;
        }
        
    }else if (indexPath.section == 1) {
        return 50;
    }else {
        return 75;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 1) {
        return 10;
    }
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (section == 2) {
        return 20.01f;
    }
    return 0.01f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            BaseInfoOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseInfoOneCell"];
            if (!cell) {
                cell = [[BaseInfoOneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BaseInfoOneCell"];
                cell.backgroundColor = [UIColor whiteColor];
            }
            
            [cell updateImageViewInfoModel:self.basicsModel];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        }else {
            BaseInfoTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseInfoTwoCell"];
            if (!cell) {
                cell = [[BaseInfoTwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BaseInfoTwoCell"];
                cell.backgroundColor = [UIColor whiteColor];
            }
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell updateCellInfoWithModelWithIndexPath:indexPath baseInfoModel:self.basicsModel];
            
            return cell;
        }
    }else if (indexPath.section == 1) {
        BaseInfoTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseInfoTwoCell"];
        if (!cell) {
            cell = [[BaseInfoTwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BaseInfoTwoCell"];
            cell.backgroundColor = [UIColor whiteColor];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell updateCellInfoWithModelWithIndexPath:indexPath baseInfoModel:self.basicsModel];
        
        return cell;
    }else {
        BaseInfoThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseInfoThreeCell"];
        if (!cell) {
            cell = [[BaseInfoThreeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BaseInfoThreeCell"];
            cell.backgroundColor = [UIColor whiteColor];
        }
        if (self.basicsModel.maritalId.length) {
            [cell updateTiJiaoUI];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = ListMaoBackgroundColor;
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = ListMaoBackgroundColor;
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {  //选择婚姻状态
        
        [HFQIndicationView showInView:self.view];
        [MyRequestTools getDicDataWithdicKey:@"marriStatus" Success:^(id  _Nullable responseObject) {
            [HFQIndicationView dismiss];
            if ([[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] isEqualToString:@"200"]){
                
                self.marriedDataArr = [responseObject objectForKey:@"data"];
                if (self.marriedDataArr.count) {
                    
                    selectRow = 0;
                    [self showPickerView];
                }
            }
        } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
            [HFQIndicationView dismiss];
            
        }];
        
        
    }else if (indexPath.section == 2) { //提交
        
        [self tiJiaoAction];
    }
}

- (void)tiJiaoAction {
    
    if (self.basicsModel.maritalId.length) {
        [HFQIndicationView showInView:self.view];
        NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:self.basicsModel.maritalId,@"maritalId", nil];
        [MyRequestTools MyInfoBasicWithParameters:param Success:^(id  _Nullable responseObject) {
            [HFQIndicationView dismiss];
            [self backAction];
            
        } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
            [HFQIndicationView dismiss];
            
        }];
    }else {
        [HUD_Mao showCharacterMessage:@"请选择婚姻状态"];
    }
    
}

-(void)showPickerView {
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenHeight)];
    backgroundView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.4/1.0];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancleAction:)];
    [backgroundView addGestureRecognizer:tap];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 240, ScreenHeight, 240)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [backgroundView addSubview:whiteView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(80, 0, ScreenWidthMao - 80 - 50, 50);
    [whiteView addSubview:leftBtn];
    
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.frame = CGRectMake(0, 0, 80, 50);
    [leftLabel setTextAlignment:NSTextAlignmentCenter];
    leftLabel.text = @"取消";
    leftLabel.textColor = HexColor(0x454444);
    leftLabel.font = [UIFont boldSystemFontOfSize:16];
    [whiteView addSubview:leftLabel];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(ScreenWidthMao - 80, 0, 80, 50);
    [rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    [rightBtn setTitleColor:ThemeMaoColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [rightBtn addTarget:self action:@selector(sureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [whiteView addSubview:rightBtn];
    
    
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, ScreenWidthMao, 190)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    [whiteView addSubview:_pickerView];
    
    [UIView animateWithDuration:0.3 animations:^{
        [[HFQCommonTools lastWindow] addSubview:backgroundView];
    }];
    
}

- (void)sureBtnAction:(UIButton *)sender{
    
    if (self.marriedDataArr.count) {
        NSDictionary *dic = self.marriedDataArr[selectRow];
        self.basicsModel.maritalId = dic[@"key"];
        self.basicsModel.maritalValue = dic[@"value"];
    }
    
    [self.mainTableView reloadData];
    UIView *whiteView = sender.superview;
    UIView *backgroundView = whiteView.superview;
    
    [UIView animateWithDuration:0.3 animations:^{
        backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        backgroundView.hidden = YES;
        [backgroundView removeFromSuperview];
    }];
}

- (void)cancleAction:(UITapGestureRecognizer *)gesture {
    
    UIView *view = gesture.view;
    [UIView animateWithDuration:0.3 animations:^{
        
        view.alpha = 0;
    } completion:^(BOOL finished) {
        view.hidden = YES;
        [view removeFromSuperview];
    }];
}
#pragma mark --------------Picker 代理方法---------------

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return self.marriedDataArr.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    NSDictionary *dic = self.marriedDataArr[row];
    return dic[@"value"];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    selectRow = row;
}



- (UITableView *)mainTableView {
    
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenHeight - SafeAreaTopHeight) style:UITableViewStyleGrouped];
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        
        [self.view addSubview:_mainTableView];
    }
    return _mainTableView;
}

- (MyInfoBasicsModel *)basicsModel {
    
    if (!_basicsModel) {
        _basicsModel = [[MyInfoBasicsModel alloc] init];
    }
    return _basicsModel;
}

-(NSArray *)marriedDataArr{
    if (!_marriedDataArr) {
        _marriedDataArr = [[NSArray alloc]init];
    }
    return _marriedDataArr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
